package YAPserver.logic;

import java.util.ArrayList;
import java.util.HashMap;

import YAPenvironment.YAPconstant;
import YAPserver.gameObjects.GameBall;
import YAPserver.gameObjects.Mod;
import YAPserver.gameObjects.Point;
import YAPserver.gameObjects.Slider;
import YAPserver.gameObjects.GameWall;
import YAPserver.mods.*;

/**
 * This model is mainly used for debugging, hence the name. It is important for this 
 * model to be as flexible as possible, but at the same time be as controllable as possible.
 * Unlike in the StandardModel there are no Randomisers. When a Ball or Mod is created 
 * if not specified how then default values are to be used.
 * The class is used to test extremes.
 * @author Daniel
 *
 */

public class DebugModel extends AbstractModel {

	public static int numOfBalls = 0;
	public static int numOfEscapees = 0;
	public static int numOfCollisions = 0;
	
	private int m_defaultTicks = 400;	//default number of rounds until a new mod is created
	private Point m_startPoint = new Point(300, 300);
	
	/**
	 * Constructs a new DebugModel
	 */
	public DebugModel(){
		super();
		resetTicks();
		m_maxMods = 0;
		
		defaultBallSpeed = YAPconstant.C_BALLSTARTSPEED;
		defaultBallRadius = YAPconstant.C_BALLRADIUS;
		defaultModSpeed = YAPconstant.C_BALLSTARTSPEED;
		defaultModRadius = YAPconstant.C_BALLRADIUS;
	}
	
	@Override
	//a simple implemenatation of assignplayers
	public void assignPlayers(HashMap<Integer, Player> players,
			ArrayList<Slider> sliders, ArrayList<GameWall> walls) {
		//default
		int i = 0;
		for(int id : players.keySet()){
			//first create sliders
			//then assign the walls and sliders to the player
			sliders.add(new Slider(walls.get(i)));
			players.get(id).setWallAndSlider(walls.get(i),sliders.get(i));
			i++;
		}
	}
	
	@Override
	public void addBall(ArrayList<GameBall> balls) {
		balls.add(new GameBall(m_startPoint, Math.PI/2., 3, 5));
		numOfBalls++;
	}
	
	@Override
	public void deleteBall(ArrayList<GameBall> balls, ArrayList<Mod> mods) {
		for(int i=0; i<balls.size(); i++){
			if(balls.get(i).isOnField()==false){
				balls.remove(i);
			}else{
				//TODO: get rid of magic numbers
				//if the ball is outside of the screen then delete
				if(balls.get(i).getMiddlePoint().getXPos() < -50
						|| balls.get(i).getMiddlePoint().getXPos() > 650
						|| balls.get(i).getMiddlePoint().getYPos() < -50
						|| balls.get(i).getMiddlePoint().getYPos() > 650){
					balls.remove(i);
					numOfEscapees++;
//					System.out.println("total balls: " + numOfBalls + " total escapees: " + numOfEscapees);
				}
			}
		}
		for(int i=0; i<mods.size(); i++){
			if(mods.get(i).isActivated()){
				mods.remove(i);
			}else
			if(mods.get(i).getMiddlePoint().getXPos() < 0 - 50
					|| mods.get(i).getMiddlePoint().getXPos() > 600 + 50
					|| mods.get(i).getMiddlePoint().getYPos() < 0 - 50
					|| mods.get(i).getMiddlePoint().getYPos() > 600 + 50){
//				System.out.println("mod angle: "+mods.get(i).getLine().getAngle());
				mods.remove(i);
				numOfEscapees++;
			}
		}
	}
	
	/*
	 * at the moment I only add 8-balls, but in the future I shall make it possible to
	 * choose from a list (non-Javadoc)
	 * @see YAPserver.gameLogic.IGameModel#createMod(java.util.ArrayList, YAPserver.gameLogic.GameModifier)
	 */
	@Override
	public void createMod(ArrayList<Mod> modsMoving, GameModifier gameMod) {
//		modsMoving.add(new Mod(gameMod, new ModEightBall()));
		modsMoving.add(new Mod(m_startPoint, Math.PI/2, 3, 5, gameMod, new ModSafetyNet()));
	}	

	@Override
	void resetTicks() {
		m_ticks = m_defaultTicks;	
	}

}
